﻿using System;
using UnityEngine;
using Island;

class GameHandler : IHandler
{
    public void RegisterHandler(HandlerCenter center)
    {
        center.AddHandler(Protocol.ReqLocation,OnLocation);
        center.AddHandler(Protocol.ReqApplyFriend,OnApplyFriend);
        center.AddHandler(Protocol.ReqAgreeFriend,OnAgreeFriend);
        center.AddHandler(Protocol.RspAddFriend,OnAddFriend);
    }
    
    private void OnLocation(byte[] data)
    {
        PBLocation loc = Util.Deserilize<PBLocation>(data);
        Debug.Log("Id:"+loc.id + "N:" + loc.latitude +"E:"+ loc.longitude);
        ActorManager.Instance.GetActor(loc.id).SetLocation(loc);
    }

    private void OnApplyFriend(byte[] data)
    {
        PBFriend pb = Util.Deserilize<PBFriend>(data);

        MessageBox.Show("有人想添加你为好友！","好友申请",
            (result) => {
                switch (result)
                {
                    case DialogResult.Yes:
                        pb.type = FriendType.AGREE;
                        break;
                    case DialogResult.No:
                        pb.type = FriendType.REFUSE;
                        break;
                }
                NetworkManager.Instance.Send(Protocol.ReqAgreeFriend, pb);
            }, MessageBoxButtons.YesNo);
    }

    private void OnAgreeFriend(byte[] data)
    {
        PBFriend pb = Util.Deserilize<PBFriend>(data);
        switch (pb.type)
        {
            case FriendType.AGREE:
                MessageBox.Show("已添加对方为好友!");
                break;
            case FriendType.REFUSE:
                MessageBox.Show("对方拒绝了好友申请!");
                break;
        }
    }

    /// <summary>
    /// 同意对方请求后，提示是否已添加
    /// </summary>
    private void OnAddFriend(byte[] data)
    {
        PBFriend pb = Util.Deserilize<PBFriend>(data);
        MessageBox.Show("好友已经添加！");
    }

}
